﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DaZhong.app_code;
using DaZhong.Properties;
using WeiTap.Article.Dal;
using WeiTap.SearchEngin.Sogou;

namespace DaZhong
{
    public partial class ArticelTag : BaseForm
    {
        public frmDefault ParentFrmDefault { get; set; }
        public ArticelTag()
        {
            InitializeComponent();
            this.textBox = txtMessage;
        }

        private async void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                var name = txtTagName.Text;
                if (string.IsNullOrEmpty(name))
                {
                    Echo(Resources.EmptyTextValue + Environment.NewLine);
                    return;
                }
                var tag = await ArticelTagManager.GetTag(name);

                if (tag != null)
                {
                    Trace.WriteLine(tag.Id + "|" + tag.WtAccounts + Environment.NewLine);
                    var tagAcs = tag.WtAccounts.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();

                    var acs = await WeiTap.SearchEngin.Sogou.SogouClient.SearchWeiChatAccount(tag.Name);
                    foreach (var ac in acs)
                    {
                        if (tagAcs.Exists(a => a.Equals(ac.AccountCode, StringComparison.OrdinalIgnoreCase)))
                            continue;
                        tagAcs.Add(ac.AccountCode);
                        

                    }

                    tag.WtAccounts = string.Join(",", tagAcs);
                    await ArticelTagManager.Update(tag);
                    if (acs.Count > 0)
                    {
                        Echo(string.Join(Environment.NewLine,acs.Select(t=>t.AccountCode)));
                        Echo("更新成功");
                    }
                        
                }
                else
                {
                    Echo("未找到相关的标签" + Environment.NewLine);

                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }


        }


    }
}
